package com.leve.control.util;

import com.alibaba.fastjson.JSON;
import com.leve.control.domain.SmsSendRequest;
import com.leve.control.domain.SmsSendResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

/**
 * @author whongyu
 * @create by 2019/6/12
 */
@Slf4j
public class SmsUtil {

    @Resource
    private static RedisUtil redisUtil;

    // 用户平台API账号
    public static String account = "N531435_N3104345";
    // 用户平台API密码
    public static String password = "7xRfdZMeBg338a";

    public static int sendSms(String moblie) {
        //请求地址请登录253云通讯自助通平台查看或者询问您的商务负责人获取
        String smsSingleRequestServerUrl = "http://smsbj1.253.com/msg/send/json";
        // 短信内容
        int a = (int)((Math.random()*9+1)*100000);
        redisUtil.set(moblie,a);
        String msg = "【力维共享植保】你好,你的验证码是"+a;
        //状态报告
        String report= "true";

        SmsSendRequest smsSingleRequest = new SmsSendRequest(account, password, msg, moblie,report);

        String requestJson = JSON.toJSONString(smsSingleRequest);

        log.info("手机验证码发送成功!手机号"+moblie+":验证码为："+ a);

        System.out.println("before request string is: " + requestJson);

        String response = HttpUtils.sendSmsByPost(smsSingleRequestServerUrl, requestJson);

        System.out.println("response after request result is :" + response);

        SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);

        System.out.println("response  toString is :" + smsSingleResponse);

        return a;
    }

}
